<?php

namespace App\Common\Model\Base;

use App\Common\Model\Attribute\CreateSql;
use App\Common\Model\Attribute\InitData;
use App\Common\Model\Model;

/**
 * 管理员 
 * 
 * @property int    $id           
 * @property string $name         用户名
 * @property string $account      账号
 * @property string $password     密码
 * @property int    $error_number 密码错误次数
 * @property string $lately_time  最近登录
 * @property string $error_date   错误日期
 * @property int    $status       状态
 * @property array  $role_ids     角色
 * @property int    $create_id    创建人
 * @property string $create_time  创建时间
 * @property string $update_time  修改时间
 * @property int    $delete_time  删除时间 
 */
#[CreateSql(<<<SQL
CREATE TABLE `sc_administrators` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(32) NOT NULL DEFAULT '' COMMENT '用户名',
  `account` varchar(32) NOT NULL DEFAULT '' COMMENT '账号',
  `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
  `error_number` tinyint(2) NOT NULL DEFAULT '0' COMMENT '密码错误次数',
  `lately_time` datetime DEFAULT NULL COMMENT '最近登录',
  `error_date` date DEFAULT NULL COMMENT '错误日期',
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态：1=正常，2=冻结',
  `role_ids` json  DEFAULT NULL  COMMENT '角色',
  `create_id` int(11) NOT NULL DEFAULT '0' COMMENT '创建人',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '修改时间',
  `delete_time` int(11) DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `account` (`account`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='管理员';
SQL
)]
#[InitData([
    ['id' => 1, 'name' => '超管', 'account' => 'admin', 'password' => '$2y$10$q.Pv4rJMlb9kznF4zZn6KO6GxkSt5ojOCjUo8xjzBXrT2Tahrbyly', 'error_number' => 0, 'lately_time' => '2020-01-01 00:00:00', 'status' => 1, 'role_ids' => '[]', 'create_id' => 1, 'create_time' => '2020-01-01 00:00:00', 'update_time' => '2020-01-01 00:00:00']
])]
class Administrators extends Model
{

    protected $schema = [
        'id'           => 'int',
        'name'         => 'varchar',
        'account'      => 'varchar',
        'password'     => 'varchar',
        'error_number' => 'tinyint',
        'lately_time'  => 'datetime',
        'error_date'   => 'date',
        'status'       => 'tinyint',
        'role_ids'     => 'varchar',
        'create_id'    => 'int',
        'create_time'  => 'datetime',
        'update_time'  => 'datetime',
        'delete_time'  => 'int',
    ];

    protected $hidden = [
        'password',
        'delete_time'
    ];

    protected $type = [
        ...self::DEFAULT_CASTS,
        'role_ids' => 'array'
    ];

    protected  $field = ["name", "account", "password", "error_number", "lately_time", "error_date", "status", "role_ids", "create_id"];
}
